package com.ruoyi.project.stop.controller;

import java.util.List;

import com.ruoyi.project.stop.domain.Remove;
import com.ruoyi.project.stop.domain.SensorRecordVO;
import com.ruoyi.project.stop.dto.SensorRecordDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.stop.domain.SensorRecord;
import com.ruoyi.project.stop.service.ISensorRecordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 传感器数据记录Controller
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@RestController
@RequestMapping("/stop/sensor")
public class SensorRecordController extends BaseController
{
    private String prefix = "stop/sensor";

    @Autowired
    private ISensorRecordService sensorRecordService;

    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询传感器数据记录列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SensorRecordVO sensorRecordVO)
    {
        startPage();
        //List<SensorRecord> list = sensorRecordService.selectSensorRecordList(sensorRecord);
        List<SensorRecordDTO> list = sensorRecordService.selectSensorRecordDTOList(sensorRecordVO);
        return getDataTable(list);
    }

    /**
     * 导出传感器数据记录列表
     */
    @Log(title = "传感器数据记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SensorRecord sensorRecord)
    {
        List<SensorRecord> list = sensorRecordService.selectSensorRecordList(sensorRecord);
        ExcelUtil<SensorRecord> util = new ExcelUtil<SensorRecord>(SensorRecord.class);
        return util.exportExcel(list, "传感器数据记录数据");
    }

    /**
     * 新增传感器数据记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存传感器数据记录
     */
    @Log(title = "传感器数据记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SensorRecord sensorRecord)
    {
        return toAjax(sensorRecordService.insertSensorRecord(sensorRecord));
    }

    /**
     * 修改传感器数据记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SensorRecord sensorRecord = sensorRecordService.selectSensorRecordById(id);
        mmap.put("sensorRecord", sensorRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存传感器数据记录
     */

    @Log(title = "传感器数据记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SensorRecord sensorRecord)
    {
        return toAjax(sensorRecordService.updateSensorRecord(sensorRecord));
    }

    /**
     * 删除传感器数据记录
     */

    @Log(title = "传感器数据记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Remove remove)
    {
        return toAjax(sensorRecordService.deleteSensorRecordByIdsAndTime(remove));
    }
}
